package com.woniuxy.light_smart.service.imp;

import com.woniuxy.light_smart.dto.ApplicationDto;
import com.woniuxy.light_smart.entity.ApplicationRole;
import com.woniuxy.light_smart.entity.UserRole;
import com.woniuxy.light_smart.mapper.ApplicationRoleMapper;
import com.woniuxy.light_smart.service.ApplicationRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ApplicationRoleServiceImpl implements ApplicationRoleService {
   @Resource
   private ApplicationRoleMapper applicationRoleMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return applicationRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ApplicationRole record) {
        return applicationRoleMapper.insert(record);
    }

    @Override
    public int insertSelective(ApplicationRole record) {
        return applicationRoleMapper.insertSelective(record)
                ;
    }

    @Override
    public ApplicationRole selectByPrimaryKey(Long id) {
        return applicationRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ApplicationRole record) {
        return applicationRoleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ApplicationRole record) {
        return applicationRoleMapper.updateByPrimaryKey(record);
    }

    @Override
    @Transactional
    public Integer insertOne(ApplicationDto applicationDto) {
        List<ApplicationRole> roles = applicationRoleMapper.selectAllApplication(applicationDto.getUserId(),applicationDto.getAreaId(),applicationDto.getCheckRole());
        if(roles.size()>0){
            return -1;
        }
        return applicationRoleMapper.insertOne(applicationDto);
    }

    @Override
    public List<ApplicationRole> selectAppRoleOne() {
        return applicationRoleMapper.selectAppRoleOne();
    }

    @Override
    public List<UserRole> selectAllUserRole(ApplicationDto applicationDto) {
        UserRole userRole = new UserRole();
        userRole.setUserId(applicationDto.getUserId());
        userRole.setRoleId(applicationDto.getRoleId());

        List<UserRole> roles = applicationRoleMapper.selectAllUserRole(applicationDto.getUserId());
        roles.add(userRole);
        return roles;
    }

    @Override

    public int insertUserRole(List<UserRole> roles) {
        int i=applicationRoleMapper.deleteUserRole(roles.get(0).getUserId());
        if(i>0){
            List<Integer> list = new ArrayList<Integer>();
            for (int j = 0; j < roles.size(); j++) {
                list.add(roles.get(j).getRoleId());

            }
            return applicationRoleMapper.insertUserRole(roles.get(0).getUserId(),list);
        }else {
            return -1;
        }

    }

    @Override
    public int updateUserRole(ApplicationDto applicationDto) {
        ApplicationRole applicationRole = new ApplicationRole();
        applicationRole.setAppRole(applicationDto.getAppRole());
        applicationRole.setFlag(applicationDto.getFlag());
        int i = applicationRoleMapper.updateByPrimaryKeySelective(applicationRole);
        return i;
    }
}
